package com.terenac;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.hardware.Camera;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraForm extends Activity{
        Bitmap bmp;
        Uri mUri;
        double longitude;
        double latitude;
        Message message = new Message();
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.cameraresult);
                
                LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

                startCamera();
        
        }
        
        @Override
        protected void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
                
                 //Upload button
                 Button btn_upload = (Button) findViewById(R.id.buttonUpload);   
                 btn_upload.setOnClickListener(new OnClickListener()
                 {
                        public void onClick(View v) 
                        {
                                createMessage();
                                if ( PostService.sendMessage(message) ){
                                    
                                	Context context = getApplicationContext();
                                    int duration = Toast.LENGTH_SHORT;
                                    Toast toast = Toast.makeText(context, "Upload ok.", duration);
                                    toast.show();
                                	
                                }else {
                                	
                                	Context context = getApplicationContext();
                                    int duration = Toast.LENGTH_SHORT;
                                    Toast toast = Toast.makeText(context, "Upload not ok.", duration);
                                    toast.show();
								}
                                
                        }
                 });
        }



        private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
            }

                public void onProviderDisabled(String provider) {
                        // TODO Auto-generated method stub
                        
                }

                public void onProviderEnabled(String provider) {
                        // TODO Auto-generated method stub
                        
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {
                        // TODO Auto-generated method stub
                        
                }
        };

        private void createMessage(){ 
                EditText desc = (EditText) findViewById(R.id.editTextDesc);
                String description = desc.getText().toString().trim();  
                message.setDescription(description);
                message.setPicture(bmp);
                message.setLatitude(latitude);
                message.setLongitude(longitude);
        }
        
        private void startCamera(){
                final int CAMERA_PIC_REQUEST = 1337;
                
                mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "imgnm_"+  String.valueOf(System.currentTimeMillis())+ ".jpg"));
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mUri);
                cameraIntent.putExtra("return-data", true);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
        
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                // TODO Auto-generated method stub
                super.onActivityResult(requestCode, resultCode, data);
                setContentView(R.layout.cameraresult);
                if (resultCode == RESULT_OK){
                        
                	/*
            		Bundle extras = data.getExtras();
                    bmp = (Bitmap) extras.get("data");
                    ImageView image = (ImageView) findViewById(R.id.ivPicture);
                    image.setImageBitmap(bmp);
                      */
                	
                    String path = mUri.getPath();

                    if (path.length() > 0) {
                                        String filepath = path;
                                        String filename = filepath.substring(filepath.lastIndexOf("/") + 1,filepath.length());
                                            String filetype = ".jpg";
                                            //Bitmap bm = BitmapFactory.decodeFile(filepath);
                                            //mg_view.setImageBitmap(bm);
                                            bmp = BitmapFactory.decodeFile(filepath);
                                        }
                    ImageView image = (ImageView) findViewById(R.id.ivPicture);
                    image.setImageBitmap(bmp);

                }
        }
        
}